'''
Author: LostPatrol_3
Date: 2023-11-04 17:19:44
LastEditors: LostPatrol_3
LastEditTime: 2023-11-08 17:27:47
Description: Frame of client GUI. No function implement
'''
from PyQt5 import QtGui
from PyQt5.QtWidgets import QDesktopWidget
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QHBoxLayout
from PyQt5.QtWidgets import QListWidget
from PyQt5.QtWidgets import QTextEdit
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QToolBar
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QAction
from PyQt5.QtWidgets import QLabel
from PyQt5.QtGui import QPixmap
from PyQt5.QtGui import QIcon
from PyQt5.QtGui import QFont
from PyQt5.QtCore import Qt
import sys
import os

# framework for client GUI, contains no functionality

WIDTH = 1000
HEIGHT = 650
IP = '8.130.106.82'
PORT = '12345'

class MyFrame(QMainWindow):
    """
        GUI framework for client
    """
    #######################################################
    #             #                                       #
    #   ####      #                                       #
    #             #                                       #
    #             #                                       #
    #             #                                       #
    #  ServerIP   #                                       #
    #  ServerPort #                                       #
    #  UserName   #                                       #
    #   connect   #                                       #
    #  disconnec  #########################################
    #             #                                       #
    #             #                                       #
    #             #                                       #
    #             #                            ##    ##   #
    #######################################################
    def __init__(self, id:int=0, userName:str='Chatter1'):
        super(MyFrame, self).__init__()
        self.id = id
        self.userName = userName
        self.port = PORT
        self.ip = IP
        self.w = WIDTH
        self.h = HEIGHT
        self.currentDirectory = os.path.dirname(os.path.abspath(__file__))

        self.createComponentOfSettings()
        self.createComponentOfChat()
        self.createLayoutOfChat()
        self.createLayoutOfSettings()

        self.configComponentOfSettings()
        self.configComponentOfChat()
        self.configToolBar()
        self.initMainWindow()

    def getPicturePath(self, name:str)->str:
        imagePath = os.path.join(self.currentDirectory, 'picture', name)
        return imagePath
    
    def getFilePath(self, name:str)->str:
        filePath = os.path.join(self.currentDirectory, 'files', name)
        return filePath
    
    def createComponentOfChat(self):
        self.QTextEdit_chatBox = QTextEdit()
        self.QToolbar_toolBar = QToolBar('chat tool bar')
        self.QAction_file = QAction(QIcon(self.getPicturePath('file')),'File', self)
        self.QAction_picture = QAction(QIcon(self.getPicturePath('picture')),'Picture', self)
        self.QToolbar_toolBar.addSeparator()
        self.QToolbar_toolBar.addAction(self.QAction_file)
        self.QToolbar_toolBar.addSeparator()
        self.QToolbar_toolBar.addAction(self.QAction_picture)
        self.QToolbar_toolBar.addSeparator()

        self.QTextEdit_chatBox = QTextEdit()
        self.QTextEdit_inputBox = QTextEdit()
        self.QPushButton_send = QPushButton('Send')
        self.QPushButton_clear = QPushButton('Clear')


    def createComponentOfSettings(self):
        self.QPixmap_logo = QPixmap(self.getPicturePath('user'))
        self.QLabel_user = QLabel(self)

        self.QLabel_serverIP = QLabel('IP')
        self.QLabel_serverPort = QLabel('Port')

        self.QLineEdit_serverIP = QLineEdit(self)
        self.QLineEdit_serverPort = QLineEdit(self)
        self.QLabel_userName = QLabel('Chatter1')
        self.QLineEdit_userName = QLineEdit(self)
        
        self.QPushButton_applyServerIP = QPushButton('Apply')
        self.QPushButton_applyServerPort = QPushButton('Apply')
        self.QPushButton_connect = QPushButton('Connect')
        self.QPushButton_disconnect = QPushButton('Disconnect')

        self.QLabel_members = QLabel('Online members:')
        self.QListWidget_memberList = QListWidget(self)
    

    def createLayoutOfChat(self):
        self.QHBox_send = QHBoxLayout()
        self.QHBox_send.addWidget(self.QPushButton_clear)
        self.QHBox_send.addWidget(self.QPushButton_send)

        self.QVBox_chat = QVBoxLayout()
        self.QVBox_chat.addWidget(self.QTextEdit_chatBox)
        self.QVBox_chat.addWidget(self.QToolbar_toolBar)
        self.QVBox_chat.addWidget(self.QTextEdit_inputBox)
        self.QVBox_chat.addLayout(self.QHBox_send)


    def createLayoutOfSettings(self):
        self.QVBox_user = QVBoxLayout()
        self.QVBox_user.addWidget(self.QLabel_user)
        self.QVBox_user.addWidget(self.QLabel_userName)
        self.QVBox_user.addWidget(self.QLineEdit_userName)

        self.QHBox_serverIP = QHBoxLayout()
        self.QHBox_serverIP.addWidget(self.QLabel_serverIP)
        self.QHBox_serverIP.addWidget(self.QLineEdit_serverIP)
        self.QHBox_serverIP.addWidget(self.QPushButton_applyServerIP)

        self.QHBox_serverPort = QHBoxLayout()
        self.QHBox_serverPort.addWidget(self.QLabel_serverPort)
        self.QHBox_serverPort.addWidget(self.QLineEdit_serverPort)
        self.QHBox_serverPort.addWidget(self.QPushButton_applyServerPort)

        self.QHBox_connect = QHBoxLayout()
        self.QHBox_connect.addWidget(self.QPushButton_connect)
        self.QHBox_connect.addWidget(self.QPushButton_disconnect)

        self.QVBox_members = QVBoxLayout()
        self.QVBox_members.addWidget(self.QLabel_members)
        self.QVBox_members.addWidget(self.QListWidget_memberList)

        self.QVBox_settings = QVBoxLayout()
        self.QVBox_settings.addLayout(self.QVBox_user)
        self.QVBox_settings.addStretch(0)
        self.QVBox_settings.addLayout(self.QHBox_serverIP)
        self.QVBox_settings.addLayout(self.QHBox_serverPort)
        self.QVBox_settings.addLayout(self.QHBox_connect)
        self.QVBox_settings.addStretch(0)
        self.QVBox_settings.addLayout(self.QVBox_members)
        self.QVBox_settings.addStretch(0)


    def configComponentOfSettings(self):
        self.QPixmap_logo = self.QPixmap_logo.scaled(130, 130, Qt.KeepAspectRatio)
        self.QLabel_user.setGeometry(0, 0, 130, 130)
        self.QLabel_user.setPixmap(self.QPixmap_logo)
        self.QLabel_user.setAlignment(Qt.AlignCenter)

        self.QLabel_serverPort.setFixedWidth(60)
        self.QLabel_serverIP.setFixedWidth(60)
        self.QLineEdit_serverPort.setFixedWidth(150)
        self.QLineEdit_serverIP.setFixedWidth(150)
        self.QPushButton_applyServerPort.setFixedWidth(50)
        self.QPushButton_applyServerIP.setFixedWidth(50)
        self.QPushButton_connect.setFixedWidth(100)
        self.QPushButton_disconnect.setFixedWidth(100)
        self.QLabel_members.setFixedWidth(180)
        self.QListWidget_memberList.setFixedWidth(300)

        self.QLineEdit_serverIP.setText(self.ip)
        self.QLineEdit_serverPort.setText(self.port)


        font = QFont('Microsoft YaHei', 20, QFont.Bold)
        self.QLabel_userName.setFont(font)
        self.QLabel_userName.setFixedWidth(300)
        self.QLabel_userName.setAlignment(Qt.AlignCenter)# QLabel.setAlignment 是将QLabel的内容居中

        self.QLineEdit_userName.setFont(font)
        self.QLineEdit_userName.setFixedWidth(350)
        self.QLineEdit_userName.setAlignment(Qt.AlignCenter)
        self.QLineEdit_userName.setHidden(True)
        
        self.QVBox_user.setAlignment(Qt.AlignCenter)
        self.QHBox_connect.setAlignment(Qt.AlignCenter)
        self.QVBox_settings.setAlignment(Qt.AlignTop)

        font = QFont('Microsoft YaHei', 12, QFont.Bold)
        self.QLabel_members.setFont(font)
        self.QListWidget_memberList.setStyleSheet(
            'QListWidget { background-color: #f5f5f5; border: 0px solid #000000; color: #000000; }'
            'QListWidget::item:selected { background-color: #aaffaa; }'
        )
        self.QLineEdit_userName.setStyleSheet(
            'QLineEdit { border: 0px solid #000000; color: #000000; }'
        )


    def configComponentOfChat(self):
        self.QTextEdit_inputBox.setFixedHeight(140)
        self.QTextEdit_chatBox.setReadOnly(True)
        self.QHBox_send.setAlignment(Qt.AlignRight)
        self.QTextEdit_chatBox.setStyleSheet(
            'QTextEdit { border: 0px solid #000000; color: #000000; }'
        )
        self.QTextEdit_inputBox.setStyleSheet(
            'QTextEdit { border: 0px solid #000000; color: #000000; }'
        )


    def configToolBar(self):
        pass
    

    def initMainWindow(self):
        self.topLayout = QHBoxLayout()
        self.topLayout.addLayout(self.QVBox_settings)
        self.topLayout.addLayout(self.QVBox_chat)

        self.central_widget = QWidget()
        self.central_widget.setLayout(self.topLayout)
        self.setCentralWidget(self.central_widget)

        self.setWindowTitle('ChatRoom v1.0')
        self.setWindowIcon(QIcon(self.getPicturePath('logo')))
        
        screenSize = QDesktopWidget().screenGeometry()
        ax = (screenSize.width() - self.w) // 2
        ay = (screenSize.height() - self.h) // 2
        self.setGeometry(0,0, self.w, self.h)
        self.move(ax, ay)
        self.show()

        
if __name__ == "__main__":
    app = QApplication(sys.argv)
    font = app.font()
    font.setFamily('Microsoft YaHei')
    app.setFont(font)

    mainWindow = MyFrame()
    mainWindow.show()
    app.exec_()